package com.whfp.coky.utils

import com.tencent.mmkv.MMKV
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.entry.model.ActivationToke
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import java.text.SimpleDateFormat
import java.util.*

/**
 *
 * 项目内部信息构造，转换
 * created by coky on 2022/4/7
 */
class InfoTool {



    companion object{

        const val secretHead = "shadow@zxsw2021@zxsm2021"
        const val PRE_ACTIVATION_INFO_YTJ = "activationInfo_YTJ"
        //设备序列号（SN）
        const val CT_DEVICE_CODE = "ctDdeviceCode"
        //设备的MAC地址
        const val CT_DEVICE_MAC = "ctDeviceMac"
        //设备ID（扫描激活二维码获取）
        const val CT_DEVICE_ID = "ctDeviceId"
        //接口地址（扫描激活二维码获取）
        const val LINK_SYS = "linkSys"
        //定位经度
        const val LAT = "lat"
        //定位纬度
        const val LNG = "lng"
        //定位地址描述
        const val LOCATION = "location"
        /**
         * 构造本地唯一识别
         */
        fun buildPhoneMac(): String {
            var phoneMac = MMKV.defaultMMKV().getString(Constants.PHONE_MAC, "")
            if (phoneMac.isNullOrEmpty()) {
                phoneMac = SimpleDateFormat("yyyyMMddHHmmssSSS").format(Date())
                MMKV.defaultMMKV().putString(Constants.PHONE_MAC,phoneMac)
            }
            return phoneMac!!
        }

        /**
         * 获取加密编码
         *
         * @param claims 要加密的数据
         * @param secret 加密的秘钥
         * @return
         */
        fun generateDescToken(token: ActivationToke, secret: String = secretHead + token.phoneMac): String? {
            return Jwts.builder()
                .setClaims(actToken2Map(token))
                .signWith(SignatureAlgorithm.HS256, secret.toByteArray())
                .compact()
        }

        private fun actToken2Map(activationToken: ActivationToke):Map<String?, Any?>?{
            return HashMap<String?, Any?>().apply {
                this[CT_DEVICE_ID] = activationToken.phoneMac
                this[CT_DEVICE_MAC] = activationToken.phoneMac
                this[CT_DEVICE_CODE] = activationToken.phoneMac
                this[LINK_SYS] = activationToken.linkSys
                this[LAT] = activationToken.lat
                this[LNG] = activationToken.lng
                this[LOCATION] = activationToken.loction
            }
        }
    }
}